package com.lzp.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.lzp.service.AuthorService;
import com.lzp.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class AuthorServiceImpl implements AuthorService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public String auth(String username, String password) {
        String s = SecureUtil.md5(password);//使用MD5算法对密码加密
        String md5Secure = "202cb962ac59075b964b07152d234b70";
        if("lisi".equals(username) && md5Secure.equals(s)) {
            //1. 生成一个uuid字符串
            String uuid = UUID.randomUUID().toString();
            //2. 使用uuid加一个固定key
            redisTemplate.opsForValue().set("login-token:" + uuid, username);
            //3. 为jwt创建一个map数据
            Map<String, Object> map = new HashMap<>();
            map.put("username", username);
            map.put("password", password);
            map.put("token", uuid);
            //4. 为jwt生成一个token字串
            String token = JwtUtils.createToken(map);

            //5. 返回
            return token;
        }

        return null;
    }
}
